﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// LocManager 的摘要描述
/// </summary>
public class LocManager
{
    public LocManager() : this(string.Empty)
    {
        //
        // TODO: 在此加入建構函式的程式碼
        //
    }

    public LocManager(string dbName)
    {
        //
        // TODO: 在此加入建構函式的程式碼
        //
        this.DBName = dbName;
    }

    protected string DBName;

    Dictionary<string, DataTable> _Data = new Dictionary<string, DataTable>();

    public string GetText(string tableName, string valueCol, params object[] keys)
    {
        var info = GetTableLocInfo(tableName, valueCol);
        var keyinfo = GetTableKeyInfo(tableName);

        if (!_Data.ContainsKey(tableName + valueCol))
        {
            var db = new DB(DBName);

            _Data.Add(tableName + valueCol, db.Execute(string.Format("SELECT {0}, {1} FROM {2}", info["TextCol"]
                , MyObj.JoinFormat(",","{0}",keyinfo.DefaultView,"KeyCol"), tableName)));
        }
        
        var keylist = new List<string>();
        
        for(int i = 0; i < keys.Length; i++)
        {
            var dr = keyinfo.Rows[i];

            keylist.Add(string.Format("{0}=Convert('{1}','{2}')", dr["KeyCol"], keys[i], dr["DataType"]));
        }

        var result = _Data[tableName + valueCol].Select(MyObj.JoinFormat(" AND ", "{0}", keylist));

        if (result.Length == 0)
        {
            return string.Empty;
        }
        else
        {
            return result[0][info["TextCol"].ToString()].ToString();
        }
    }

    public DataRow GetTableLocInfo(string tableName, string valueCol)
    {
        var db = new DB(DBName);

        var dr = db.ExecuteRow("SELECT * FROM TableLoc WHERE TableName = @TableName AND ValueCol = @ValueCol AND Locale = @Locale "
                    , new DBParameter("TableName", tableName), new DBParameter("ValueCol", valueCol), new DBParameter("Locale", CurrentLocale));

        return dr;
    }

    public DataTable GetTableKeyInfo(string tableName)
    {
        var db = new DB(DBName);

        var dt = db.Execute("SELECT * FROM TableKey WHERE TableName = @TableName ORDER BY Seq "
                    , new DBParameter("TableName", tableName));

        return dt;
    }

    public string GetTextCol(string tableName, string valueCol)
    {
        return GetTableLocInfo(tableName, valueCol)["TextCol"].ToString();
    }

    public void Translate(DataRow dr, string tableName, string valueCol)
    {
        Translate(dr.Table, tableName, valueCol);
    }

    public void Translate(DataTable dt, string tableName, string valueCol)
    {
        var db = new DB(DBName);
        var textCol = GetTableLocInfo(tableName, valueCol)["TextCol"].ToString();

        dt.Columns.Add(valueCol);

        dt.Columns[valueCol].Expression = textCol;

        //var locdata = db.Execute(string.Format("SELECT {0} FROM {1} WHERE 
    }

    public string CurrentLocale
    {
        get
        {
            return HttpContext.Current.Request.UserLanguages[0];
        }
    }

    public static LocManager Current
    {
        get
        {
            if (HttpContext.Current.Items["LocManager"] == null)
            {
                HttpContext.Current.Items["LocManager"] = new LocManager();
            }

            return HttpContext.Current.Items["LocManager"] as LocManager;
        }
    }

    public static LocManager AttendanceCurrent
    {
        get
        {
            if (HttpContext.Current.Items["AttendanceCurrent"] == null)
            {
                HttpContext.Current.Items["AttendanceCurrent"] = new LocManager("Attendance");
            }

            return HttpContext.Current.Items["AttendanceCurrent"] as LocManager;
        }
    }
}